import { RouteRecordRaw } from "vue-router";
import { dashboardBreadcrumb } from "@/router/routes/dashboard";

const pathObj = {
  base: "基础",
  button: "按钮",
  card: "卡片",
  breadcrumb: "面包屑",
  dropdown: "下拉菜单",
  dialog: "弹窗",

  form: "表单",
  input: "输入框",
  select: "选择器",
  radio: "单选框",
  checkbox: "复选框",
};

// 生成 views 目录的对应路由，components 目录下的 vue 文件忽略
export const createViewRoutes: (floder: string) => RouteRecordRaw[] = (
  floder = ""
) => {
  // require.context 第一个参数不能为变量
  const pages = (<any>require).context("@/views", true, /\.vue$/);
  const routes: RouteRecordRaw[] = [];
  const pageKeys = pages
    .keys()
    .filter((p) => p.includes(`${floder}/`) && !p.includes("components/"));

  pageKeys.forEach((key) => {
    const path = key.split("/")[2];
    const name = pathObj[path] || path;

    routes.push({
      path: `/${path}`,
      component: () => import(`@/views/${floder}/${path}/${path}.vue`),
      children: [],
      meta: {
        title: name,
        isKeepAlive: true,
        breadcrumbList: [
          ...dashboardBreadcrumb,
          { name: pathObj[floder] || floder, code: "" },
          { name: name, code: path },
        ],
        icon: "icon-shouye1-copy",
      },
    });
  });

  return routes;
};
